package cn.nju.sirx.algorithm.sort;

/**
 * Selection sort.
 * 
 * @author SirX
 */
public class SelectionSort implements ISort {

    @Override
    public <T extends Comparable<? super T>> void sort(T[] data) {
        for (int i = 0; i < data.length - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < data.length; j++) {
                if (data[j].compareTo(data[minIndex]) < 0) {
                    minIndex = j;
                }
            }
            // swap
            if (i != minIndex) {
                T tmp = data[i];
                data[i] = data[minIndex];
                data[minIndex] = tmp;
            }
        }
    }

}
